package com.guigu.qingcheng.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.goods.TbStockBack;
import com.guigu.qingcheng.pojo.order.TbOrderItem;
import com.guigu.qingcheng.servicegoods.mapper.TbSkuMapper;
import com.guigu.qingcheng.servicegoods.mapper.TbStockBackMapper;
import com.guigu.qingcheng.servicegoods.service.TbStockBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbStockBackServiceImpl extends ServiceImpl<TbStockBackMapper, TbStockBack> implements TbStockBackService {
    @Resource
    private TbStockBackMapper mapper;

    @Autowired
    private TbStockBackService stockBackService;

    @Resource
    private TbSkuMapper skuMapper;

    //根据条件查询封装查询对象
    public QueryWrapper<TbStockBack> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbStockBack> wapper = new QueryWrapper<TbStockBack>();
        if(searchMap != null){
            if(searchMap.get("orderId") != null && !"".equals(searchMap.get("orderId")))
                wapper.eq("order_id", searchMap.get("orderId"));
            if(searchMap.get("skuId") != null && !"".equals(searchMap.get("skuId")))
                wapper.eq("sku_id", searchMap.get("skuId"));

            if(searchMap.get("num") != null && !"".equals(searchMap.get("num")))
                wapper.eq("num", searchMap.get("num"));
            if(searchMap.get("status") != null && !"".equals(searchMap.get("status")))
                wapper.eq("status", searchMap.get("status"));
            if(searchMap.get("createTime") != null && !"".equals(searchMap.get("createTime")))
                wapper.like("create_time", searchMap.get("createTime"));
            if(searchMap.get("backTime") != null && !"".equals(searchMap.get("backTime")))
                wapper.eq("back_time", searchMap.get("backTime"));
        }
        return wapper;
    }

    @Override
    public Page<TbStockBack> findPage(Map<String, Object> searchMap, int page, int size) {
        Page<TbStockBack> pageTbStockBack = new Page<TbStockBack>(page, size);
        QueryWrapper<TbStockBack> query = queryWrapper(searchMap);
        IPage<TbStockBack> list = this.stockBackService.page(pageTbStockBack, query);
        return pageTbStockBack;
    }

    @Override
    public List<TbStockBack> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbStockBack> wrapper = queryWrapper(searchMap);
        List<TbStockBack> list = this.stockBackService.list(wrapper);
        return list;
    }

    @Override
    @Transactional
    public void addList(List<TbOrderItem> orderItems) {
        for (TbOrderItem orderItem:orderItems) {
            TbStockBack stockBack=new TbStockBack();
            stockBack.setOrderId(orderItem.getOrderId());
            stockBack.setSkuId(orderItem.getSkuId());
            stockBack.setStatus("0");
            stockBack.setNum(orderItem.getNum());
            stockBack.setCreateTime(new Date());
            mapper.insert(stockBack);

        }
    }

    @Override
    @Transactional
    public void doBack() {
        System.out.println("库存回滚任务开始");
        //查询库存回滚表中状态为0的记录
        QueryWrapper<TbStockBack> wrapper = new QueryWrapper<>();
        wrapper.eq("status","0");
        List<TbStockBack> list = mapper.selectList(wrapper);
        for (TbStockBack stockBack:list) {
            //添加库存
            skuMapper.deductionStock(stockBack.getSkuId(),-stockBack.getNum());
            //减少销量
            skuMapper.addSaleNum(stockBack.getSkuId(),-stockBack.getNum());
            stockBack.setStatus("1");
            stockBack.setBackTime(new Date());
            mapper.updateById(stockBack);
        }
        System.out.println("库存回滚任务结束");
    }
}
